////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.logging.targets
{

import ru.goodcore.logging.ILogger;
import ru.goodcore.logging.LogEvent;
//import ru.goodcore.logging.ILogger;

/**
 *  All logger target implementations that have a formatted line style output
 *  should extend this class.
 *  It provides default behavior for including date, time, category, and level
 *  within the output.
 *
 *
 * @author				Dimarik
 * @version				1.0
 * @playerversion		9
 * @langversion			3.0
 */

public class LineFormattedTarget extends AbstractLoggingTarget
{
    include "../../../../includes/core/Version.as";

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

	/**
	 *  Constructor.
	 *
	 *  <p>Constructs an instance of a logger target that will format
	 *  the message data on a single line.</p>
	 */
	public function LineFormattedTarget() {
		super();
	}

	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  fieldSeparator
	//----------------------------------

	[Inspectable(category="General", defaultValue=" ")]

	/**
	 *  The separator string to use between fields (the default is " ")
	 */
	public var fieldSeparator:String = " ";

	//----------------------------------
	//  includeCategory
	//----------------------------------

	[Inspectable(category="General", defaultValue="false")]

	/**
	 *  Indicates if the category for this target should added to the trace.
	 */
	public var includeCategory:Boolean;

	//----------------------------------
	//  includeDate
	//----------------------------------

	[Inspectable(category="General", defaultValue="false")]

	/**
	 *  Indicates if the date should be added to the trace.
	 */
	public var includeDate:Boolean;

	//----------------------------------
	//  includeLevel
	//----------------------------------

	[Inspectable(category="General", defaultValue="false")]

	/**
	 *  Indicates if the level for the event should added to the trace.
	 */
	public var includeLevel:Boolean;

	//----------------------------------
	//  includeTime
	//----------------------------------

	[Inspectable(category="General", defaultValue="false")]

	/**
	 *  Indicates if the time should be added to the trace.
	 */
	public var includeTime:Boolean;

	//--------------------------------------------------------------------------
	//
	//  Protected methods
	//
	//--------------------------------------------------------------------------

	/**
	 *  Descendants of this class should override this method to direct the 
	 *  specified message to the desired output.
	 *
	 *  @param  message String containing preprocessed log message which may
	 *              include time, date, category, etc. based on property settings,
	 *              such as <code>includeDate</code>, <code>includeCategory</code>,
	 *          etc.
	 */
	protected function internalLog(message:String):void {
		//throw new GenericError(GenericErrorCode.METHOD_MUST_BE_OVERRIDDEN);
	}

	//--------------------------------------------------------------------------
	//
	//  Overridden methods
	//
	//--------------------------------------------------------------------------

	/**
	 *  This method handles a <code>LogEvent</code> from an associated logger.
	 *  A target uses this method to translate the event into the appropriate
	 *  format for transmission, storage, or display.
	 *  This method is called only if the event's level is in range of the
	 *  target's level.
	 * 
	 *  @param event The <code>LogEvent</code> handled by this method.
	 */
	public override function logEvent(event:LogEvent):void {
		var message:String = this.formatMessage(event);
		this.internalLog(message);
	}

	//-------------------------------------------------------------------------
	//
	//  Private methods
	//
	//-------------------------------------------------------------------------

	public function formatMessage(event:LogEvent):String {
		var date:String = '';

		if (this.includeDate || this.includeTime) {
			var d:Date = new Date();

			if (this.includeDate) {
				date = Number(d.getMonth() + 1).toString() + '/' +
				   d.getDate().toString() + '/' + 
				   d.getFullYear() + this.fieldSeparator;
			}

			if (this.includeTime) {
				date += this.padTime(d.getHours()) + ':' +
				this.padTime(d.getMinutes()) + ':' +
				this.padTime(d.getSeconds()) + '.' +
				this.padTime(d.getMilliseconds(), true) + this.fieldSeparator;
			}

		}

		var level:String = '';
		if (this.includeLevel) {
			level = '[' + LogEvent.getLevelString(event.level) +
			']' + this.fieldSeparator;
		}

		var category:String = this.includeCategory ?
			ILogger(event.target).category + this.fieldSeparator : '';

		return (date + level + category + event.message);
	}

	/**
	 *  @private
	 */
	private function padTime(num:Number, millis:Boolean = false):String {
		if (millis) {
			if (num < 10)
				return '00' + num.toString();
			else if (num < 100)
				return '0' + num.toString();
			else 
				return num.toString();
		}
		else {
			return num > 9 ? num.toString() : '0' + num.toString();
		}
	}

}
}
